package org.itboys.mobile.service.mongo.front;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayFootprintDto;
import org.itboys.mobile.entity.mongo.SundayFootprint;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mysql.web.product.SundayProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by zhangyang on 2017/12/26.
 * 圣代_足迹_service
 */
@Service
public class SundayFrontFootprintService extends BaseMongoService<SundayFootprint>{

    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayFootprint> getEntityClass() {
        return SundayFootprint.class;
    }

    @Autowired
    private SundayProductService productService;
    @Autowired
    private ResourceHolder resourceHolder;


    /**
     * 生成访问足迹
     * @param memberId  用户id
     * @param productId 商品id
     */
    public void save(Long memberId,Long productId){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("memberId",memberId);
        param.put("productId",productId);
        long oldCount = super.count(param);
        //相同的商品和用户只产生一条访问足迹
        if(oldCount>0){
            SundayFootprint footprint = new SundayFootprint();
            footprint.setMemberId(memberId);
            footprint.setProductId(productId);
            super.save(footprint);
        }/*else{//重复访问同一个商品,更新足迹的访问时间
            SundayFootprint footprint = footprintList.get(0);
            Long time = new Date().getTime();
            footprint.setUt(time);
            super.updateExceptEmpty(footprint.getId(),footprint);
        }*/

    }

    /**
     * 获取用户足迹
     * @param memberId  用户id
     * @return  足迹集合
     */
    public List<SundayFootprintDto> select(Long memberId, HttpServletRequest request){
        Map<String,Object> param = ParamUtil.packageMongoExactParam(request, "");
        param.put("memberId",memberId);
        param.put("isDeleted", SundayCommonConstants.NO);

        //按足迹访问时间倒序排
        param.put("sort","ut");
        param.put("order","DESC");
        PageResult<SundayFootprint> pageResult = super.pageQuery(request,param);
        List<SundayFootprintDto> dtos = packageMuilt(pageResult.getData());
        return dtos;
    }

    /**
     * 组装dto(多)
     * @param footprints
     * @return
     */
    public List<SundayFootprintDto> packageMuilt(List<SundayFootprint> footprints){
        List<SundayFootprintDto> dots = new ArrayList<SundayFootprintDto>();
        String imgRoot = resourceHolder.getStringValue("imgRoot");
        //匹配商品
        List<Long> productIds = new ArrayList<>();
        for(SundayFootprint footprint:footprints){
            productIds.add(footprint.getProductId());
        }
        if(ListUtil.isNotNull(productIds)){
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("ids",productIds);
            List<SundayProduct> products =productService.select(param);
            if(ListUtil.isNotNull(products)){
                for(SundayProduct product : products){
                    for(SundayFootprint footprint : footprints){
                        if(LongUtil.isNotZreo(footprint.getProductId())&&
                                product.getId().longValue()==footprint.getProductId().longValue()){
                            SundayFootprintDto dto = new SundayFootprintDto();
                            dto.setProductId(product.getId());
                            dto.setProductImage(imgRoot + product.getImage());
                            dto.setProductName(product.getName());
                            dto.setProductPrice(product.getPrice().setScale(2,BigDecimal.ROUND_HALF_UP).toEngineeringString());
                            dto.setStandard(product.getStandard());
                            dots.add(dto);
                            break;
                        }
                    }
                }
            }
        }
        return dots;
    }
}
